<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\PermissionRequest;
use App\Http\Resources\PermissionCollectionResource;
use App\Http\Resources\PermissionResource;
use App\Models\Permission;

class PermissionController extends Controller
{
    /**
     * 获取基础信息
     * @return mixed
     */
    public function params()
    {
        $permissions = Permission::select(['id', 'label', 'level'])->get()->map(function ($permission) {
            $permission['label'] = str_repeat('├─', $permission['level'] - 1) . $permission['label'];
            return $permission;
        });

        return $this->success($permissions);
    }

    /**
     * 权限列表
     * @return mixed
     */
    public function index()
    {
        $permissions = Permission::with('children')->where('pid', 0)->get()->prepend([
            'label' => '名称',
            'name' => '标识',
            'description' => '描述',
            'ops' => '操作'
        ]);
        return $this->success(new PermissionCollectionResource($permissions));
    }

    /**
     * 权限详情
     * @param Permission $permission
     * @return mixed
     */
    public function show(Permission $permission)
    {
        return $this->success(new PermissionResource($permission));
    }

    /**
     * 新增权限
     * @param PermissionRequest $request
     * @return mixed
     */
    public function store(PermissionRequest $request)
    {
        $data = $this->checkData($request);

        if (!is_array($data)) { // 出错了返回错误信息
            return $data;
        }
        Permission::create($data);

        return $this->created('权限新增成功');
    }

    /**
     * 更新权限
     * @param PermissionRequest $request
     * @param Permission $permission
     * @return mixed
     */
    public function update(PermissionRequest $request, Permission $permission)
    {
        $data = $this->checkData($request);

        if (!is_array($data)) { // 出错了返回错误信息
            return $data;
        }
        $permission->fill($data);
        $permission->save();

        return $this->success(new PermissionResource($permission));
    }

    /**
     * 检查输入数据并返回
     * @param $request
     * @return mixed
     */
    protected function checkData($request)
    {
        $data = $request->only(['name', 'label', 'description', 'pid']);

        if ($pid = $data['pid'] ?? 0) {
            $parent = Permission::find($pid);
            if (empty($parent)) {
                return $this->failed('找不到父级权限信息');
            }
        }

        return array_merge($data, ['level' => ($parent['level'] ?? 0) + 1]);
    }

    /**
     * 删除权限
     * @param Permission $permission
     * @return mixed
     */
    public function destroy(Permission $permission)
    {
        try {
            if ($permission->roles()->exists()) {
                return $this->failed('请先删除拥有此权限的角色');
            }
            $permission->delete();
            return $this->success('权限删除成功');
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }
}
